package com.ice.app.system.menu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iceframework.core.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.entity.Menu;
import com.ice.app.system.menu.service.MenuService;
import com.ice.app.system.menu.vo.MenuTreeVo;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;
import com.iceframework.core.utils.WebUtils;

/**
 * 菜单
 * 
 * @author Palo
 *
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService menuService;

	/**
	 * 更新页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getMenu(@RequestParam(value = "key") String key, HttpServletRequest request) {
		Menu menu = menuService.getMenu(key);
		return Result.success().add("menu", menu);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param menu
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	public Result menuIndexData(Menu menu, Page<Menu> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<Menu> menus = menuService.getMenus(menu);
		PageInfo<Menu> data = new PageInfo<Menu>(menus, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param menu
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveMenu(Menu menu) throws Exception {
		if (menuService.saveMenu(menu)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delMenu(String keys) {
		if (menuService.deleteMenus(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 获取首页菜单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/nav/tree", method = RequestMethod.GET)
	public List<MenuTreeVo> getNavBarMenu() {
		return menuService.getMenuTree(WebUtils.getUserName());
	}

	/**
	 * 菜单树数据
	 * 
	 * @param pn
	 * @param menu
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/tree/list/data", method = RequestMethod.POST)
	public List<Menu> getMenuTreeListData(Menu menu, HttpServletRequest request) {
		return menuService.getMenus(menu);
	}
	

	/**
	 * ztree加载菜单数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ztree/data", method = RequestMethod.GET)
	public Result menuZTreeIndexData(HttpServletRequest request) {
		List<MenuTreeVo> menuTree = menuService.getMenuZTreeByUserId(WebUtils.getUserId());
		return Result.success().add("data", menuTree);
	}

}
